<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .temp-check-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .img{
   width: 150px;
   height: 100px;
   float: left;
   margin: 5px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>短信模板审核</span>
    </div>
    <div class="line"></div>
    <temp-check-form
      class="temp-check-form"
      @on-success-valid="handleSubmit">
    </temp-check-form>
    <temp-check-drawer
      :modalState="modalState"
      :row="row"
      @on-cancel="cancel"
      @temp-check-valid="handleEdit">
    </temp-check-drawer>
    <check-confirm-modal
      :modalState="confirmState"
      @cancel-confirm="cancelConfirm"
      @confirm-valid="handleConfirm"></check-confirm-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TempCheckForm from '_c/temp-check-form'
import TempCheckDrawer from '_c/temp-check-drawer'
import CheckConfirmModal from '_c/check-confirm-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    TempCheckForm,
    TempCheckDrawer,
    CheckConfirmModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '财务管理商户账户列表',
      index: '',
      confirmData: {},
      loading: true,
      modalState: false,
      confirmState: false,
      playload: null,
      drawer: null,
      rows: 20,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '短信模板列表',
          key: 'list',
          minWidth: 310,
          align: 'center',
          render: (h, params) => {
            return h('span', `【${params.row.tag}】${params.row.content}`)
          }
        },
        // {
        //   title: '内部销售',
        //   key: 'salename',
        //   width: 200,
        //   align: 'center'
        // },
        {
          title: '审核状态',
          key: 'state',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state === '0' ? '未审核' : params.row.state === '1' ? '审核通过' : params.row.state === '2' ? '未通过' : '-')
          }
        },
        {
          title: '申请日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.ctime ? params.row.ctime : '-')
          }
        },
        // {
        //   title: '审核日期',
        //   key: 'ctime',
        //   minWidth: 150,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('span', params.row.ctime ? params.row.ctime : '-')
        //   }
        // },
        {
          title: '备注',
          key: 'des',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.des ? params.row.des : '-')
          }
        },
        // {
        //   title: '更新时间',
        //   key: 'updatetime',
        //   align: 'center',
        //   width: 180
        // },
        {
          title: '操作',
          key: 'cz',
          minWidth: 196,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row)
                  }
                }
              }, '审核')
              // h('Button', {
              //   props: {
              //     type: 'error',
              //     size: 'small',
              //     ghost: 'ghost'
              //   },
              //   style: {
              //     marginRight: '5px'
              //   },
              //   on: {
              //     click: () => {
              //       this.handleDelete(params.index)
              //     }
              //   }
              // }, '删除')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      tempCheckList: state => state.templateCheck.tempCheckList,
      dataCount: state => state.templateCheck.tempCheckDataCount,
      historyData: state => state.templateCheck.tempCheckHistoryData,
      pageSize: state => state.templateCheck.tempCheckPageSize,
      row: state => state.templateCheck.row
    })
  },
  methods: {
    ...mapActions([
      'getTempCheckList',
      'getTempCheckCurrentRow',
      'editTempCheckData',
      'changeTempCheckPage',
      'changeTempCheckPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      // console.log(this.playload)
      this.getTempCheckList(playload).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (row) {
      // console.log(row)
      let newRow = {}
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getTempCheckCurrentRow(newRow)
      this.modalState = true
    },
    async handleEdit (playload) {
      this.drawer = playload.drawer
      this.confirmData = {
        id: playload.id,
        state: playload.state,
        des: playload.des
      }
      this.modalState = false
      this.confirmState = true
    },
    async handleConfirm () {
      this.loading = true
      const resp = await this.editTempCheckData(this.confirmData)
      if (resp.data.state === '000') {
        this.$Message.success('操作成功')
        this.getTempCheckList({
          page: this.page,
          rows: this.rows,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.confirmState = false
      this.drawer.resetFields()
      // this.updateConfirmModalState(true)
    },
    cancel (value) {
      this.modalState = false
      value.resetFields()
    },
    cancelConfirm () {
      this.confirmState = false
      this.modalState = true
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeTempCheckPage(index)
      this.getTempCheckList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeTempCheckPageSize(value)
      this.changePage(this.page)
      this.getTempCheckList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getTempCheckList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
